<?php

namespace app\model\agency;

use app\base\BaseModel;

/**
 * 模型-AgencyOrderItemModel
 * Class AgencyOrderItemModel
 * @package app\model
 */
class AgencyOrderItemModel extends BaseModel
{
    protected $name = 'agency_order_item';

    /**
     * 范围查询-order_out_handle_status
     * @param $query
     * @param string $op
     * @param $status
     */
    public function scopeHandleStatus($query, $op = '=', $status)
    {
        if (!empty($status)) {
            $query->where('d.handle_status', $op, $status);
        }
    }

    /**
     * 获取订单项的数据
     * @param $order_id
     * @param string $pay_sn
     * @return mixed
     */
    public function getChildInfo($order_id, $pay_sn = "")
    {
        //订单项
        if (!empty($pay_sn)) {
            $this->where("order_id", $order_id)->update(['pay_sn' => $pay_sn]);
            $data = $this->where("order_id", $order_id)->select();
        } else {
            $data = $this
                ->alias("c")
                ->leftJoin("t_goods b", "c.goods_id=b.id")
                ->where("c.order_id", $order_id)
                ->field("c.*,b.goods_title,b.goods_img,c.goods_price,c.goods_number,c.goods_freight,c.goods_type")
                ->select();
        }
        return $data;
    }

    /**
     * 获取用户订单项信息
     * @param $order_id
     * @return mixed
     */
    public function getItemsInfo($order_id)
    {
        $data = $this
            ->alias("a")
            ->join("t_goods b", "a.goods_id=b.id")
            ->where("a.order_id", $order_id)
            ->field("a.goods_id,a.id,a.goods_price,a.goods_number,a.order_total,b.goods_title,b.goods_img,b.goods_storage")
            ->select();
        return $data;
    }

    /**
     * 获取订单指定商品列表
     * @param $order_id
     * @param $goods_id
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItemByGoodsId($order_id, $goods_id = '')
    {
        $map = [
            'order_id' => $order_id
        ];
        if (empty($goods_id)) {
            $items = $this->where($map)->select();
        } else {
            if (is_array($goods_id)) {
                $items = $this->where($map)->whereIn('goods_id', implode(',', $goods_id))->select();
            } else {
                $map['goods_id'] = $goods_id;
                $items = $this->where($map)->select();
            }
        }
        if (empty($items)) {
            $items = [];
        }
        return $items;
    }
}
